#!/usr/bin/env python

from showcase_class import Showcase

def init ( liwe, admin = 0 ):
        from liwe import parts

	Showcase ( liwe )

        parts.add ( liwe.layman, "ext",  )
        parts.add_wwl ( liwe.layman, [ "base" ] )

        liwe.layman.add_css ( "modules/showcase/gfx/showcase.css" )
        liwe.layman.add_js ( "modules/showcase/js/showcase.js" )
        liwe.layman.add_js_code ( "showcase.init ();" )

        if admin and int ( admin ):
                liwe.layman.add_css ( "modules/showcase/gfx/showcase.admin.css" )
                liwe.layman.add_js ( "modules/showcase/js/showcase.admin.js" )
                liwe.layman.add_js_code ( "showcase.admin.init ();" )

	return ''


# def box ( liwe, tags = '', cnt_name = 'SC-showcase', timer = 0 ): tags no longer supperted use ``id_conf`` or ``name``
def box ( liwe, id_conf = 0, name = '', cnt_name = 'SC-showcase' ):
	# FIXME: da togliere quando il parser del system e' piu' figo
	if not id_conf and not name: name = 'home'

	sc = Showcase ( liwe )
	conf = sc.get_conf ( id = id_conf, name = name )

	if not conf: return ''

	id_conf = conf [ 'id' ]
	name    = conf [ 'name' ]

	img_res = sc.list ( id_conf = id_conf, conf_name = name ) 
	if not img_res [ 'rows' ] : return ''
	img = img_res [ 'rows' ] [ 0 ]

	img [ 'cnt_name' ] = cnt_name
	img [ 'descr-div' ] = ''
	img [ 'title' ] = ''

	descr = img.get ( 'descr', '' ).strip ()
	if descr:
		img [ 'title' ] = sc.templates [ 'SC-title' ] % { "descr" : descr }
		img [ 'descr-class' ] = 'descr'
		img [ 'descr-div' ] = sc.templates [ 'SC-descr' ] % img
	else:
		img [ 'descr-div' ] = ''

	res = sc.templates [ 'SC-showcase' ] % img

        liwe.layman.add_js_code ( "showcase.start('%s','%s')" % ( cnt_name, conf [ 'id' ] ) )

	return res
